namespace DotNetNuke.UI.Utilities
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Web.UI;

    /// -----------------------------------------------------------------------------
    /// Project	 : DotNetNuke
    /// Class	 : ClientAPIPostBackControl
    /// 
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// Control used to register post-back events
    /// </summary>
    /// <remarks>
    /// In order for a post-back event to be trapped we need to associate a control to 
    /// handle the event.
    /// </remarks>
    /// <history>
    /// [Jon Henning]	9/15/2004	Created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class ClientAPIPostBackControl : Control, IPostBackEventHandler
    {
        private static List<WeakReference> __ENCList = new List<WeakReference>();
        private Hashtable m_oEventHandlers;

        public ClientAPIPostBackControl()
        {
            List<WeakReference> refObjectHelperL0 = __ENCList;
            lock (refObjectHelperL0)
            {
                __ENCList.Add(new WeakReference(this));
            }
            this.m_oEventHandlers = new Hashtable();
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="objPage">Page</param>
        /// <param name="strEventName">Event Name</param>
        /// <param name="objDelegate">Delegate for Function of type PostBackEvent</param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [Jon Henning]	9/15/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public ClientAPIPostBackControl(Page objPage, string strEventName, PostBackEvent objDelegate)
        {
            List<WeakReference> refObjectHelperL0 = __ENCList;
            lock (refObjectHelperL0)
            {
                __ENCList.Add(new WeakReference(this));
            }
            this.m_oEventHandlers = new Hashtable();
            ClientAPI.GetPostBackClientEvent(objPage, this, "");
            this.AddEventHandler(strEventName, objDelegate);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds a postback event handler to the control
        /// </summary>
        /// <param name="strEventName">Event Name</param>
        /// <param name="objDelegate">Delegate for Function of type PostBackEvent</param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [Jon Henning]	9/15/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void AddEventHandler(string strEventName, PostBackEvent objDelegate)
        {
            if (!this.m_oEventHandlers.Contains(strEventName))
            {
                this.m_oEventHandlers.Add(strEventName, objDelegate);
            }
            else
            {
                this.m_oEventHandlers[strEventName] = (PostBackEvent) Delegate.Combine((PostBackEvent) this.m_oEventHandlers[strEventName], objDelegate);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Function implementing IPostBackEventHandler which allows the ASP.NET page to invoke
        /// the control's events
        /// </summary>
        /// <param name="strEventArgument"></param>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [Jon Henning]	9/15/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public void RaisePostBackEvent(string strEventArgument)
        {
            ClientAPIPostBackEventArgs objArg = new ClientAPIPostBackEventArgs(strEventArgument);
            if (this.get_EventHandlers(objArg.EventName) != null)
            {
                this.get_EventHandlers(objArg.EventName)(objArg);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Property to access individual post back event handlers based off of event name
        /// </summary>
        /// <param name="strEventName">Event Name</param>
        /// <returns>PostBackEvent</returns>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [Jon Henning]	9/15/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public PostBackEvent this[string strEventName]
        {
            get
            {
                if (this.m_oEventHandlers.Contains(strEventName))
                {
                    return (PostBackEvent) this.m_oEventHandlers[strEventName];
                }
                return null;
            }
        }



        public PostBackEvent get_EventHandlers(string strEventName)
        {
            if (m_oEventHandlers.Contains(strEventName))
                return m_oEventHandlers[strEventName] as PostBackEvent;
            else return null;
        }

        public delegate void PostBackEvent(ClientAPIPostBackEventArgs Args);
    }
}

